/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pantallas;

import clases.GestorGrabarLeerDelDisco;
import clases.MuebleEstructura;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author DanielRH
 */
public class PantallaInicialMueble extends javax.swing.JFrame {

    /**
     * Creates new form PantallaCreacionEstructuraMueble
     */
    public PantallaInicialMueble() {
        initComponents();
        cargarTabla();
        cargarMouseListener();
        estructuraSeleccionada="";
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        jFrame2 = new javax.swing.JFrame();
        btnPiezas = new javax.swing.JButton();
        btnOperaciones = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaEstructuras = new javax.swing.JTable();
        btnNuevaEstructura = new javax.swing.JButton();
        btnModificarEstrcutura = new javax.swing.JButton();
        btnEliminarEstructura = new javax.swing.JButton();

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jFrame2.setBounds(new java.awt.Rectangle(9, 9, 9, 9));

        javax.swing.GroupLayout jFrame2Layout = new javax.swing.GroupLayout(jFrame2.getContentPane());
        jFrame2.getContentPane().setLayout(jFrame2Layout);
        jFrame2Layout.setHorizontalGroup(
            jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame2Layout.setVerticalGroup(
            jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btnPiezas.setText("Piezas del Mueble");
        btnPiezas.setEnabled(false);

        btnOperaciones.setText("Operaciones");
        btnOperaciones.setEnabled(false);

        tablaEstructuras.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tablaEstructuras);

        btnNuevaEstructura.setText("Nuevo");
        btnNuevaEstructura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevaEstructuraActionPerformed(evt);
            }
        });

        btnModificarEstrcutura.setText("Modificar");
        btnModificarEstrcutura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarEstrcuturaActionPerformed(evt);
            }
        });

        btnEliminarEstructura.setText("Eliminar");
        btnEliminarEstructura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarEstructuraActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnPiezas)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnOperaciones)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(btnEliminarEstructura, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnNuevaEstructura, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnModificarEstrcutura, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnPiezas)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnOperaciones)
                        .addGap(18, 18, 18)
                        .addComponent(btnNuevaEstructura)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnModificarEstrcutura)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEliminarEstructura))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 324, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNuevaEstructuraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevaEstructuraActionPerformed
        // TODO add your handling code here:
        PantallaNombre pantCrear=new PantallaNombre();
        pantCrear.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_btnNuevaEstructuraActionPerformed

    private void btnModificarEstrcuturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarEstrcuturaActionPerformed
        // TODO add your handling code here:
        if(estructuraSeleccionada.compareTo("")==0)
        {
            JOptionPane.showMessageDialog(getContentPane(),"No se seleccionó ningún Mueble","Error",JOptionPane.ERROR_MESSAGE);
        }
        else
        {
            MuebleEstructura muebleSeleccionado=null;
            
            GestorGrabarLeerDelDisco manejador=new GestorGrabarLeerDelDisco();
        
            LinkedList listaEstructuras=manejador.obtenerLista(new MuebleEstructura());

            Iterator iter = listaEstructuras.iterator();
            while(iter.hasNext())
            {
                MuebleEstructura aux = (MuebleEstructura)iter.next();
                if(aux.getNombre().compareTo(estructuraSeleccionada)==0)
                {
                    muebleSeleccionado=aux;
                    System.out.println("Se encontro el mueble");
                }
            }
            PantallaNombre pantCrear=new PantallaNombre();
            pantCrear.setMuebleActual(muebleSeleccionado);
            pantCrear.setVisible(true);
            this.dispose();
        }
    }//GEN-LAST:event_btnModificarEstrcuturaActionPerformed

    private void btnEliminarEstructuraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarEstructuraActionPerformed
        // TODO add your handling code here:
        if(estructuraSeleccionada.compareTo("")==0)
        {
            JOptionPane.showMessageDialog(getContentPane(),"No se seleccionó ningún Mueble","Error",JOptionPane.ERROR_MESSAGE);
        }
        else
        {
            GestorGrabarLeerDelDisco manejador=new GestorGrabarLeerDelDisco();
            boolean seElimino=manejador.eliminarObjeto(new MuebleEstructura(estructuraSeleccionada));
            if(seElimino==true)
            {
                JOptionPane.showMessageDialog(getContentPane(),"El mueble se ha eliminado correctamente.","Información",JOptionPane.INFORMATION_MESSAGE);
                cargarTabla();
            }
        }
    }//GEN-LAST:event_btnEliminarEstructuraActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PantallaInicialMueble.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PantallaInicialMueble.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PantallaInicialMueble.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PantallaInicialMueble.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PantallaInicialMueble().setVisible(true);
            }
        });
    }
    
    private void cargarTabla()
    {
        GestorGrabarLeerDelDisco manejador=new GestorGrabarLeerDelDisco();
        
        LinkedList listaEstructuras=manejador.obtenerLista(new MuebleEstructura());

        String nombresColumnas[] = {"Estructuras"};
        estructurasTablaModel = new DefaultTableModel(nombresColumnas,0);
        Iterator iter = listaEstructuras.iterator();
        while(iter.hasNext())
        {
            MuebleEstructura aux = (MuebleEstructura)iter.next();
            String datosFila[] = {aux.getNombre()};
            estructurasTablaModel.addRow(datosFila);
            
        }
        tablaEstructuras.setModel(estructurasTablaModel);
    }
    
    private void cargarMouseListener()
    {
        MouseAdapter adaptadorMouse=new MouseAdapter()
           {@Override
           public void mouseClicked(MouseEvent e) 
              {
                 int fila = tablaEstructuras.rowAtPoint(e.getPoint());
                 int columna = tablaEstructuras.columnAtPoint(e.getPoint());
                 if ((fila > -1) && (columna > -1))
                 {
                     estructuraSeleccionada=tablaEstructuras.getModel().getValueAt(fila, 0)+"";
//                     pacienteSeleccionado.setDni(Integer.parseInt(tablaEstructuras.getModel().getValueAt(fila, 0)+""));
//                     pacienteSeleccionado.setApellidoNombre(tablaEstructuras.getModel().getValueAt(fila, 1)+"");
                 }
              }
           };
        
        tablaEstructuras.addMouseListener(adaptadorMouse);
    }
    
    DefaultTableModel estructurasTablaModel;
    private String estructuraSeleccionada;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEliminarEstructura;
    private javax.swing.JButton btnModificarEstrcutura;
    private javax.swing.JButton btnNuevaEstructura;
    private javax.swing.JButton btnOperaciones;
    private javax.swing.JButton btnPiezas;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JFrame jFrame2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaEstructuras;
    // End of variables declaration//GEN-END:variables
}
